<?php
namespace Admin\Controller;
use Think\Controller;

/**
* 登录退出
*/
class CommonController extends BaseController
{
	// 登录页面
	public function login()
	{
		$this->display();
	}

	// 退出操作
	public function logout()
	{
		session_destroy();
		returnJsonStr(0, '退出成功');
	}

	// 登录检测
	public function verify_login()
	{
		$data = $_POST;
		$username = trim($data['username']);
		$password = trim($data['password']);
		if(empty($username) || empty($password))
		{
			returnJsonStr(1, '用户名或密码不能为空！');
		}
		$res = M('admin as a')
				->field('a.username, a.password, a.role_id, r.name')
				->join('jy_role as r on r.role_id = a.role_id')
				->where('a.username = "'.$username.'"')
				->find();
		if(empty($res))
			returnJsonStr(1, '用户不存在或用户名输入不正确！');

		if($res['password'] == $password)
		{
			session('admin_id', $res['id']);
			session('username', $res['username']);
			session('role_id', $res['role_id']);
			session('role_name', $res['name']);
			returnJsonStr(0, '登录成功');			
		}
		else
		{
			returnJsonStr(1, '用户名或密码输入不正确！');
		}
	}

}